package com.easonji.common.base.controller;

import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.util.HttpUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: BaseController
 * @Description: 控制器基础类
 * @Author: LiuJun
 * @date: 2018/07/07 22:12
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
public class BaseController {

    /**
     * 跳转方式：redirect
     */
    protected static String REDIRECT = "redirect:";

    /**
     * 跳转方式：forward
     */
    protected static String FORWARD = "forward:";

    /**
     * 成功响应
     * 
     * @param message
     *            响应消息
     * @return 成功响应结果
     */
    protected Response success(ResponseEnum responseEnum) {
        Response response = new Response();
        response.setSuccess(true);
        response.setCode(responseEnum.getCode());
        response.setMessage(responseEnum.getMessage());
        return response;
    }

    /**
     * 成功响应
     * 
     * @param message
     *            响应消息
     * @param data
     *            响应数据
     * @return 成功响应结果
     */
    protected Response success(ResponseEnum responseEnum, Object data) {
        Response response = new Response();
        response.setSuccess(true);
        response.setCode(responseEnum.getCode());
        response.setMessage(responseEnum.getMessage());
        response.setData(data);
        return response;
    }

    /**
     * 失败响应
     * 
     * @param code
     *            响应code
     * @param message
     *            响应消息
     * @return 失败响应结果
     */
    protected Response failure(ResponseEnum responseEnum) {
        Response response = new Response();
        response.setSuccess(false);
        response.setCode(responseEnum.getCode());
        response.setMessage(responseEnum.getMessage());
        return response;
    }

    protected Response failure(BindingResult result) {

        List<ObjectError> ls=result.getAllErrors();

        if(ls.size()>1){
            return failure(8001,ls.get(0).getDefaultMessage());
        }else{
            return failure(ResponseEnum.PARAMETER_ERROR);
        }



    }

    /**
     * 失败响应
     * 
     * @param code
     *            响应code
     * @param message
     *            响应消息
     * @return 失败响应结果
     */
    protected Response failure(Integer code, String message) {
        Response response = new Response();
        response.setSuccess(false);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    /**
     * 获取HttpServletRequest
     * 
     * @return request
     */
    protected HttpServletRequest getHttpServletRequest() {
        return HttpUtils.getRequest();
    }

    /**
     * 获取HttpServletResponse
     * 
     * @return response
     */
    protected HttpServletResponse getHttpServletResponse() {
        return HttpUtils.getResponse();
    }

    /**
     * 获取HttpSession
     * 
     * @return session
     */
    protected HttpSession getSession() {
        return HttpUtils.getRequest().getSession();
    }

    /**
     * 获取新的 HttpSession
     * 
     * @param flag
     * @return session
     */
    protected HttpSession getSession(Boolean flag) {
        return HttpUtils.getRequest().getSession(flag);
    }

    /**
     * 获取参数的value
     * 
     * @param name
     *            参数key
     * @return 参数内容
     */
    protected String getParameter(String name) {
        return HttpUtils.getRequest().getParameter(name);
    }

    /**
     * 设置参数
     * 
     * @param name
     *            参数key
     * @param value
     *            参数value
     */
    protected void setAttribute(String name, Object value) {
        HttpUtils.getRequest().setAttribute(name, value);
    }

    /**
     * 删除cookie
     * 
     * @param cookieName
     *            cookie名
     */
    protected void deleteCookieByName(String cookieName) {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                Cookie temp = new Cookie(cookie.getName(), "");
                temp.setMaxAge(0);
                this.getHttpServletResponse().addCookie(temp);
            }
        }
    }
}
